package com.qfmy.web.app.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 清风明月
 * @Package com.qfmy.common.sms
 * @date 2025/9/9
 * @descriptio  短信服务配置
 */
@SuppressWarnings("all")
@Configuration
@EnableConfigurationProperties(SmsProperties.class)
@ConditionalOnProperty(name = "aliyun.sms.endpoint")
public class SmsClientConfiguration {


    /**
     * 创建阿里云短信客户端
     */
    @Autowired
    private SmsProperties properties;

    /**
     * 创建并配置客户端实例
     *
     * @return 配置好的客户端实例
     * @throws RuntimeException 当客户端创建失败时抛出
     */
    @Bean
    public Client createClient() {
        // 配置客户端参数
        Config config = new Config();
        config.setEndpoint(properties.getEndpoint());
        config.setAccessKeyId(properties.getAccessKeyId());
        config.setAccessKeySecret(properties.getAccessKeySecret());
        // 创建并返回客户端实例
        try {
            return new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
